package com.example.framework2002a.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.framework2002a.R;
import com.example.framework2002a.adapter.NewListAdapter;
import com.example.framework2002a.adapter.NewTypeAdapter;
import com.example.framework2002a.databinding.FragmentHomeBinding;
import com.example.lib_core.bean.NewsListBean;
import com.example.lib_core.bean.NewsTypeBean;
import com.example.lib_core.factory.FragFactory;
import com.example.lib_core.factory.FragViewModel;
import com.fhz.utilsfhz.ApiResponse;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private FragViewModel fragViewModel;
    private List<NewsTypeBean> newsTypeBeans = new ArrayList<>();
    private List<NewsListBean> newsListBean = new ArrayList<>();
    private NewTypeAdapter newTypeAdapter;
    private NewListAdapter newListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false);

        fragViewModel = new ViewModelProvider(this,new FragFactory()).get(FragViewModel.class);
        fragViewModel.getNewType().observe(this, new Observer<ApiResponse<List<NewsTypeBean>>>() {
            @Override
            public void onChanged(ApiResponse<List<NewsTypeBean>> listApiResponse) {
                newsTypeBeans.addAll(listApiResponse.getData());

                newTypeAdapter = new NewTypeAdapter(newsTypeBeans);
                binding.homeRv.setAdapter(newTypeAdapter);
                binding.homeRv.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
            }
        });

        fragViewModel = new ViewModelProvider(this,new FragFactory()).get(FragViewModel.class);
        fragViewModel.getNewList().observe(this, new Observer<ApiResponse<List<NewsListBean>>>() {
            @Override
            public void onChanged(ApiResponse<List<NewsListBean>> listApiResponse) {
                newsListBean.addAll(listApiResponse.getData());

                newListAdapter = new NewListAdapter(newsListBean);
                binding.homeRv2.setAdapter(newListAdapter);
                binding.homeRv2.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
            }
        });

        binding.homeAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.homeCl.getVisibility() == View.VISIBLE){
                    binding.homeCl.setVisibility(View.GONE);
                }else{
                    binding.homeCl.setVisibility(View.VISIBLE);
                }
            }
        });

        return binding.getRoot();
    }
}